﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Node
{
    #region 定义节点外观
    public Rect rect;
    public string title;

    public GUIStyle style;
    public GUIStyle defaultStyle;
    public GUIStyle SelectedStyle;
    #endregion

    #region 常用标记
    /// <summary>
    /// 标记是否处于拖拽状态
    /// </summary>
    private bool isDrag = false;
    /// <summary>
    /// 标记是否选中该节点
    /// </summary>
    private bool isSelected = false;
    #endregion

    #region 标记输入口，输出口
    public ConnectPoint inPoint;
    public ConnectPoint outPoint;
    #endregion

    public Action<Node> OnRemoveNode;
    public Node(Vector2 position,float width,float height,GUIStyle style,GUIStyle SelectedStyle,
        GUIStyle inPointStyle,GUIStyle outPointStyle,
        Action<ConnectPoint> OnClickInPoint,Action<ConnectPoint> OnClickOutPoint,
        Action<Node> OnRemoveNode)
    {
        this.rect = new Rect(position.x,position.y,width,height);
        this.style = style;
        inPoint = new ConnectPoint(this, ConnectionPointType.In, inPointStyle, OnClickInPoint);
        outPoint = new ConnectPoint(this,ConnectionPointType.Out,outPointStyle,OnClickOutPoint);
        this.OnRemoveNode = OnRemoveNode;
        this.SelectedStyle = SelectedStyle;
        this.defaultStyle = style;
    }
    /// <summary>
    /// 拖拽时重新绘制
    /// </summary>
    /// <param name="delta"></param>
    public void Drag(Vector2 delta)
    {
        rect.position += delta;
    }
    /// <summary>
    /// 绘制节点
    /// </summary>
    public void Draw()
    {
        inPoint.Draw();
        outPoint.Draw();
        GUI.Box(rect, title, style);
    }

    /// <summary>
    /// 回调事件
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    public bool ProcessEvents(Event e)
    {
        switch (e.type)
        {
            case EventType.MouseDown:
                if (e.button==0)
                {
                    if (rect.Contains(e.mousePosition))
                    {
                        //这里标记进入拖拽状态
                        isDrag = true;
                        GUI.changed = true;
                        isSelected = true;
                        style = SelectedStyle;
                    }
                    else
                    {
                        GUI.changed = true;
                        isSelected = false;
                        style = defaultStyle;
                    }
                }
                if (e.button== 1&&isSelected&&rect.Contains(e.mousePosition))
                {
                    ProcessContextMenu();
                    e.Use();
                }
                break;
            case EventType.MouseUp:
                //标记退出拖拽状态
                isDrag = false;
                break;
            case EventType.MouseDrag:
                if (e.button==0&&isDrag)
                {
                    Drag(e.delta);
                    //阻塞进程，只能由调用者使用
                    e.Use();
                    return true;
                }
                break;
            default:
                break;
        }
        return false;
    }
    /// <summary>
    /// 节点右键菜单
    /// </summary>
    private void ProcessContextMenu()
    {
        GenericMenu genericMenu = new GenericMenu();
        genericMenu.AddItem(new GUIContent("Delete Node"), false, OnClickRemoveNode);
        genericMenu.ShowAsContext();
    }
    /// <summary>
    /// 点击移除
    /// </summary>
    private void OnClickRemoveNode()
    {
        if (OnRemoveNode!=null)
        {
            OnRemoveNode(this);
        }
    }
}
